package uvt.map.graph.algorithm.result;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;

import uvt.map.graph.interfaces.FromWithOnToElement;

public class ResultFrame extends JFrame implements ActionListener {
	
	private JComboBox<String> routeChoice;
	private DefaultListModel<String> listModel;
	private ArrayList<ArrayList<FromWithOnToElement>> list;
	
	public ResultFrame(ArrayList<ArrayList<FromWithOnToElement>> list) {
		this.list = list;
		setTitle("Result");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((int)(screen.getWidth() / 2 - 230),
				    (int)(screen.getHeight() / 2 - 200));
		setSize(460,400);
		setResizable(false);
		
		JPanel parentPanel = new JPanel(new BorderLayout());
		setContentPane(parentPanel);
		
		JPanel northPanel = new JPanel(new FlowLayout());		
		String ch[] = {"1 Route","2 Route","3 Route"};		
		routeChoice = new JComboBox<String>(ch);
		routeChoice.addActionListener(this);
		routeChoice.setBackground(Color.white);
		routeChoice.setBorder(new EmptyBorder(10,10,10,10));
		northPanel.add(routeChoice);
		northPanel.setBackground(Color.white);
		parentPanel.add(northPanel,BorderLayout.NORTH);
		
		listModel = new DefaultListModel<String>();
		JList<String> indicationsList = new JList<String>(listModel);
		indicationsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		indicationsList.setBorder(new EmptyBorder(20,20,20,20));
		JScrollPane listScroller = new JScrollPane(indicationsList);
		listScroller.setPreferredSize(new Dimension(250, 80));
		parentPanel.add(listScroller,BorderLayout.CENTER);
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent ac) {
		interpret(routeChoice.getSelectedIndex());
	}
	
	public void interpret(int selected) {
		for(FromWithOnToElement obj : list.get(selected)) {
			listModel.addElement("Get " + obj.getVehicleType() + " " + obj.getRouteName() +
					             " from station " + obj.getStationFromName() + " to " + 
					             obj.getStationToName() + " on street " + obj.getStreetName());
 			
		}
	}

}
